package com.EmployeeButler.mapper;

import com.EmployeeButler.entity.Attendance;
import com.EmployeeButler.entity.vo.AttendanceVo;
import com.EmployeeButler.entity.vo.CountVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import javax.naming.Name;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 作者
 * @since 2024-06-13
 */
@Mapper
public interface AttendanceMapper extends BaseMapper<Attendance> {

    @Select("SELECT \n" +
            "    u.name AS name, \n" +
            "    DATE_FORMAT(a.check_time, '%Y-%m') AS month, \n" +
            "    COUNT(*) / 2 AS days -- 每天上下班两次打卡\n" +
            "FROM \n" +
            "    attendance a\n" +
            "JOIN \n" +
            "    user u ON a.user_id = u.id\n" +
            "WHERE \n" +
            "    a.punch_type IN ('上班', '下班')\n" +
            "GROUP BY \n" +
            "    u.name, DATE_FORMAT(a.check_time, '%Y-%m');\n")
    @Results({
            @Result(column = "name", property = "name"),
            @Result(column = "month", property = "month"),
            @Result(column = "days", property = "days"),
    })
    List<AttendanceVo> getAttendanceShow();

    @Select("SELECT \n" +
            "    user_id, \n" +
            "    COUNT(*) AS punch_count\n" +
            "FROM \n" +
            "    attendance\n" +
            "WHERE \n" +
            "    check_time >= DATE_SUB(CURDATE(), INTERVAL 7 DAY)\n" +
            "GROUP BY \n" +
            "    user_id\n" +
            "ORDER BY \n" +
            "    punch_count DESC\n" +
            "LIMIT 5;\n")
    List<CountVo> getPushCount();
}
